/**
 * 
 */
package com.laozhao.EasySales.view;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.io.IOException;
import java.util.HashMap;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JRootPane;
import javax.swing.JSeparator;
import javax.swing.plaf.BorderUIResource;
import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import com.laozhao.EasySales.business.MasterServer;
import com.laozhao.EasySales.business.MenuUtil;
import com.laozhao.EasySales.business.ToolBarUtil;
import com.laozhao.EasySales.business.TrayUtil;
import com.laozhao.EasySales.model.entity.SysConfig;
import com.laozhao.dreamframe.util.FrameFactory;

/**
 * @author LaoZhao
 * 
 */
public class MainWindowsFrame extends JFrame   {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5135202476944048352L;
	// 中央容器
	private JPanel mainPane ;
//	private JPanel ststusPane ;
	
	private CardLayout mainLayout ;
	
	public  JProgressBar progressBar;
	
	private HashMap<String, AbstractPanel<?>> panelMap;
	

	/**
	 * @throws IOException
	 * @throws SAXException
	 * @throws ParserConfigurationException
	 * 
	 */
	public MainWindowsFrame() {
		super();
		this.setUndecorated(true);
		this.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
		initComponents();
	}

	public void init() {
		initData();
	}

	private void initData() {
//		JOptionPane.showInputDialog("Please enter the passworld");
		MasterServer masterServer = (MasterServer) FrameFactory.getInstance(MasterServer.class);
		SysConfig config = masterServer.load();
//		if (config != null && config.getEncryption().equals("0")) {
		if (config != null ) {
			String pwdString = "";
			do {
				pwdString = JOptionPane.showInputDialog("Please enter the passworld");
				if (pwdString == null) {
					System.exit(1);
				}
			} while (!config.getPassWorld().equals(pwdString));
			
		}
		WelcomePanel welcome = (WelcomePanel) FrameFactory.getInstance(WelcomePanel.class);
		welcome.init();
		addPane(welcome);
		
	}

	/**
	 * 初始化画面
	 */
	private void initComponents() {

		getContentPane().setLayout(new BorderLayout(20,10));
		// 添加菜单栏
		this.setJMenuBar(initMenuBar());
		// 添加工具栏
		getContentPane().add(ToolBarUtil.loadToolBar(),BorderLayout.NORTH); 
		JSeparator sep1=new JSeparator();           //菜单栏下面的分隔线   
	    sep1.setBounds(0, 30, 300, 1);   
	    sep1.setForeground(Color.WHITE);   
	    getContentPane().add(sep1); 

		panelMap = new HashMap<String, AbstractPanel<?>>();
		mainLayout = new CardLayout();
		mainPane = new JPanel(mainLayout);
		mainPane.setBackground(Color.GRAY);
		JPanel panel = new JPanel();
		panel.setBackground(Color.GRAY);
		mainPane.add("0",panel);
		panelMap.put("0", null);
		mainPane.setBorder(BorderUIResource.getBlackLineBorderUIResource());
		
		getContentPane().add(mainPane, BorderLayout.CENTER);

		// ////////////////////////////////////////////////////////////////
		TrayUtil.loadSystemTray();
		/////

		this.add(initProGressBar(),BorderLayout.SOUTH);

		this.setTitle("ESMS");
		this.pack();
//		repaint();	
		setSize(800, 600);
	}

	private JPanel initProGressBar() {
		JPanel statusPane = new JPanel( new BorderLayout());
		progressBar = new JProgressBar();
		statusPane.add(progressBar,BorderLayout.EAST);
		return statusPane;		
	}

	private javax.swing.JMenuBar initMenuBar() {
		MenuUtil menuUtil = (MenuUtil) FrameFactory.getInstance(MenuUtil.class);
		return menuUtil.getJMenuBar();		
	}

	public void addPane(AbstractPanel<?> comp) {
		if (!panelMap.containsKey(comp.getTitle())) {
			mainPane.add(comp.getTitle(), comp);			
		}
		if (panelMap.containsKey("0")) {
			panelMap.remove("0");
			mainPane.remove(0);
			
		}
		mainLayout.show(mainPane, comp.getTitle());
    }
	
	public void nextPanel(){
		mainLayout.next(mainPane);
	}
	
	public void previous(){
		mainLayout.previous(mainPane);
	}

}
